package com.manor.paradise.adapter

import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.manor.common.utils.DateUtil
import com.manor.common.utils.scaleDown
import com.manor.paradise.R
import com.manor.paradise.bean.MyHireBean
import com.opensource.svgaplayer.SVGAImageView
import java.math.BigDecimal
import java.text.SimpleDateFormat

class SpiritAdapter(layoutResId: Int, data: MutableList<MyHireBean.RecordsBean>?)
    : BaseQuickAdapter<MyHireBean.RecordsBean, BaseViewHolder>(layoutResId, data) {

    override fun convert(holder: BaseViewHolder, item: MyHireBean.RecordsBean) {
        val imAv = holder.getView<ImageView>(R.id.im_av)

        holder.setText(R.id.tv_collect, "已产:${item.producedCrystalOre.scaleDown(4)}")
                .setText(R.id.tv_surplus, "剩余:${item.totalCrystalOre.subtract(item.producedCrystalOre).scaleDown(4)}")


        Glide.with(context)
                .load(item.spiritImgUrl)
                .thumbnail(0.1f)
                .into(imAv)


        val tvName = holder.getView<TextView>(R.id.tv_name)
        tvName.text = item.spiritName.toString()
        val tvHysc = holder.getView<TextView>(R.id.tv_hysc)
        //Date date = simpleDateFormat.parse("2019-09-02");
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        //其他精灵：可活跃时长activeDuration - (当前时间 - 生效日期effectiveDate）
        //免费精灵：(可活跃时长activeDuration - 已活跃时间天数activeDays) > (失效日期 - 当前时间) ? (失效日期invalidDate - 当前时间) : (可活跃时长activeDuration - 已活跃时间天数activeDays)
        // tv_hysc.setText("活跃度有效期：" + item.activeDuration.subtract(BigDecimal(DateUtil.convertTimeToLong(item.reqTime).toString()).subtract(BigDecimal(DateUtil.convertTimeToLong(item.activeDays.toString()).toString()))) + "天")


        //为什么要把这种容易变动规则的计算放客户端？因为后台说后端计算会导致服务器冒烟！牛批！！！


        if (item.spiritId == 1 || item.spiritId == 2) {
            val asa: BigDecimal = item.activeDuration.subtract(item.activeDays)//可活跃时长
            val bsb: BigDecimal = BigDecimal(DateUtil.differentDays(simpleDateFormat.parse(item.currentTime), simpleDateFormat.parse(item.invalidDate.toString())).toString())
            val csc: BigDecimal = if ((asa) <= bsb) bsb else asa
            tvHysc.text = "雇佣有效期：" + csc.toString() + "天"
        } else {
            val nnn = BigDecimal(DateUtil.differentDays(simpleDateFormat.parse(item.effectiveDate),
                    simpleDateFormat.parse(item.currentTime.toString())).toString())
            if ((nnn).compareTo(BigDecimal.ZERO) < 0) {
                tvHysc.text = "雇佣有效期：" + item.activeDuration + "天"
            } else {
                val residue = if ( item.activeDuration.subtract(nnn).subtract(BigDecimal.ONE)  >= BigDecimal.ZERO)
                    item.activeDuration.subtract(nnn).subtract(BigDecimal.ONE)
                else
                    BigDecimal.ZERO

                tvHysc.text = "雇佣有效期：${residue}天"
            }

        }


        val createTime = holder.getView<TextView>(R.id.tv_createTime)
        createTime.text = "雇佣时间：" + item.createTime + ""
        val sc = item.activeDays.divide(item.activeDuration, 2, BigDecimal.ROUND_DOWN)
        val a = sc.multiply(BigDecimal("100")).intValueExact()
        val progressBarHealthy = holder.getView<ProgressBar>(R.id.progress_bar_healthy)
        progressBarHealthy.progress = a
        val tvTime = holder.getView<TextView>(R.id.tv_time)
        tvTime.text = "活跃时长$a%"

    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)
    }

    override fun onViewAttachedToWindow(holder: BaseViewHolder) {
        super.onViewAttachedToWindow(holder)
    }


}

